package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.GeneralPictureEditorActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.ImageManagementActivity;
import com.diudiutechnology.www.yadiudiu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by Administrator on 2017/7/19.
 */

public class MyAdapters   extends PagerAdapter {
    private List<String>  adList;
    private Context context;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    int Judge=0;
    public MyAdapters(Context mContext, List<String> adList,int Judge) {
        this.adList = adList;
        this.context = mContext;
        this.Judge=Judge;
        options = ImgUtils.imageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }
    public void addImet(List<String>  adList){
        this.adList=adList;
        notifyDataSetChanged();
    }
    @Override
    public Object instantiateItem(final ViewGroup container, int position) {
        final int pos = position % adList.size();
        ViewHolders holder =  new ViewHolders();
        View convertView = LayoutInflater.from(context).inflate(
                R.layout.my_adapter, null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        convertView.setLayoutParams(params);
        holder.imageView = (ImageView) convertView
                .findViewById(R.id.imageView);
        holder.deleting = (LinearLayout) convertView
                .findViewById(R.id.deleting);

        imageLoader.displayImage(Constant.HTTP+adList.get(pos), holder.imageView, options);
        container.addView(convertView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        holder.deleting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Judge==0){
                    ((ImageManagementActivity)context).processdeleting(pos,adList.get(pos));
                }else {
                    ((GeneralPictureEditorActivity)context).processdeleting(pos,adList.get(pos));
                }

            }
        });
        return convertView;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
    public class ViewHolders {
        ImageView imageView;
        LinearLayout deleting;
    }
}
